<?php
/**
 * +--------------------------------------------------------+
 * @Created by Phpstorm.
 * @Category MyStockFundCommand.php
 * @Depiction
 * @Author hwy <429208948@qq.com>
 * @Date 2022/6/30  16:16
 * +--------------------------------------------------------+
 */

namespace App\Console\Commands;


use App\Jobs\DataCapJob;
use App\Services\MyStockService;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;

class MyStockFundCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:my_stock_fund';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '资金数据';

    private $myStockService;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct(MyStockService $myStockService)
    {
        parent::__construct();

        $this->myStockService = $myStockService;
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->info("获取开始..." . date('Y-m-d H:i:s'));

        if (!class_exists('\App\Util\Stock')) {
            $this->info("获取结束..." . date('Y-m-d H:i:s'));
            return true;
        }

        $now = Carbon::now();
        if ($now->isWeekday() == false) {
            return true;
        }

        $y  = $now->year;//
        $m  = $now->month;//
        $d  = $now->day;//
        $h  = $now->hour;//
        $mi = $now->minute;//
        if ($h == 9 && $mi == 20) {  //9
            //清空缓存 永久化数据
            Cache::tags(['my_stock_fund'])->flush();
        }

        if ($h < 12) {
            $first  = Carbon::create($y, $m, $d, 9, 28);
            $second = Carbon::create($y, $m, $d, 11, 31);
            if (!$now->between($first, $second)) {
                return true;
            }
        } else {
            $first  = Carbon::create($y, $m, $d, 13);//13
            $second = Carbon::create($y, $m, $d, 15, 1);//15
            if (!$now->between($first, $second)) {
                return true;
            }
        }


        $stock_list = $this->myStockService->getStocks(2);

        $t = time();
        foreach ($stock_list as $key => $val) {
            $params['time'] = $t;
            $params['key']  = $key;
            dispatch((new DataCapJob($params))->onQueue('default'));
        }

        $this->info("获取结束..." . date('Y-m-d H:i:s'));
    }
}
